<?php
function mssql_export_struct_preview($data,$save_type,$dbname){
	switch($save_type):
		//-----------------------
		//     保存为HTML文件
		//-----------------------
		case "html":
			header("Content-type:application/html;charset=utf-8");
			header('Content-Disposition: attachment; filename="MSSQL-'.$dbname.'.html"');
			$_index='<div id="tb_index" class="shadow"><div class="tb_title"><strong>表结构索引</strong></div><ul>';
			$content="";
			
			for($i=0;$i<count($data);$i++):
				$_index.='<li title="'.$data[$i]['comment'].'"><a href="#map_'.$data[$i]['name'].'">'.$data[$i]['name'].'</a></li>';
				$content.='<a name="map_'.$data[$i]['name'].'"></a>';
				$content.= '<div  class="shadow">';
				$content.= '<div class="tb_title" style="margin-top:10px;"><strong>表名</strong>：'.$data[$i]['name']."  - ".$data[$i]['comment']."</div>\n";
				$content.= '<table class="tb" width="100%" border="0" cellpadding="0" cellspacing="0">'."\n";
					$content.= "\t".'<thead>'."\n";
					$content.= "\t".'<tr>'."\n";
						$content.= "\t\t".'<th width="200" align="left">字段名称</th>'."\n";
						$content.= "\t\t".'<th width="170" align="left">数据类型</th>'."\n";
						$content.= "\t\t".'<th width="60" align="left">主键</th>'."\n";
						$content.= "\t\t".'<th width="140" align="left">默认值</th>'."\n";
						$content.= "\t\t".'<th width="60" align="left">允许为空</th>'."\n";
						$content.= "\t\t".'<th align="left">注释</th>'."\n";
					$content.= "\t".'</tr>'."\n";
					$content.= "\t".'</thead>'."\n";
					$content.= "\t".'<tbody>'."\n";
				foreach($data[$i]['columns'] as $column):
					$content.= "\t".'<tr>'."\n";
						$content.= "\t\t".'<td>'.$column['column_name'].'</td>'."\n";
						$content.= "\t\t".'<td>'.$column['type_name'].'('.$column['max_length'].')'.'</td>'."\n";
						$content.= "\t\t".'<td>'.$column['is_identity'].'</td>'."\n";
						$content.= "\t\t".'<td>'.$column['default_definition'].'</td>'."\n";
						$content.= "\t\t".'<td>'.$column['is_nullable'].'</td>'."\n";
						$content.= "\t\t".'<td>'.$column['comment'].'</td>'."\n";
					$content.= "\t".'</tr>'."\n";
				endforeach;
				$content.= "\t".'</tbody>'."\n";
				$content.= '</table>'."\n";
				$content.= '</div>';
			endfor;
			$_index.='</ul></div>';
			$content.= '<div  class="tb_title" style="margin-top:10px;padding-right:20px;font-size:12px;text-align:right;">数据库【'.$dbname."】结构 - 创建自 MayaTools工具包 的  DataBaseStructExport. 创建日期： ".date("Y-m-d H:i:s")."</div>\n";
			$content.=$_index;
			$fname="tpl_struct_preview.html";
			$fp=fopen($fname,"r");
			$html=fread($fp,filesize($fname));
			fclose($fp);
			$html=str_replace("{#DATA_CONTENT}",$content,$html);
			echo $html;
			break;
			
		//-----------------------
		//     保存为TXT文件
		//-----------------------
		//-----------------------
		//     保存为CSV文件
		//-----------------------
	endswitch;
}